articles

Home / DeveloperSection / Articles / How to create .NET Core API? Provide an example.

How to create .NET Core API? Provide an example.

How to create .NET Core API? Provide an example.

Sanjay Goenka 476 28-Aug-2023

In the modern world of software development, building APIs has become an essential skill. API or Application Programming interface, which supports communication between different software systems, allowing for seamless data exchange and functional integration. With the advent of the .NET CORE framework, building APIs has become simpler, more efficient, and more cross-platform. In this article, we will discuss the process of creating a .NET CORE API with some simple examples.

 

Prerequisites:

Before starting to build the .NET CORE API, make sure the following tools are installed:

  • .NET CORE SDK: Download and install the latest version of the .NET CORE SDK from the official website.
  • IDE (Integrated Development Environment): You can use Visual Studio, Visual Studio Code, or any other IDE of your own choice.

 

Step 1: Create a new .NET CORE API Project:

  • Open your Command Line Interface (CLI) or IDE.
  • Navigate to the folder where you want to create the project.
  • Run the following command to create a new .NET CORE API Project.

 

// This command creates a new .NET CORE project named “MyApi”
dotnet new webapi -n MyApi

 

Step 2: Define API Controllers

  • Open your project in your IDE.
  • In the “Controllers” folder you will find the default WeatherForecastController.cs file. You can use it as an example or create a new controller according to your needs.
  • Define your API endpoint by creating methods in your controller. For Example, you can create a “ProductsController” with the Get method to retrieve a list of products:

 

using Microsoft.AspNetCore.Mvc; // Importing the necessary namespace
using System;
using System.Collections.Generic;
namespace MyApi.Controllers
{
    [ApiController] // Indicates that this class is an API controller
    [Route("api/[controller]")] // Specifies the base route for this controller
    public class ProductsController : ControllerBase // Inherits from ControllerBase
    {
        // This method handles HTTP GET requests
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get() // Method signature
        {
            // The following line returns an array of product names as a response
            return new string[] { "Product 1", "Product 2", "Product 3" };
        }
    }
}

 

Step 3: Test your API Locally

  • Build and run your API by executing the following command in the project folder:
dotnet run
  • Open a web browser or API testing tool (such as Postman) and navigate to  “https://localhost:5001/api/products”. You should see the list of the products returned in JSON format.

 

Step 4: Deploy Your API

Once you are satisfied with your .NET CORE API, you can deploy it to various hosting environments, such as AZURE, AWS, or your own server. One common deployment option is using Docker containers.

  • Create a Dockerfile in your project folder with the following content:
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /src
COPY ["MyApi.csproj", ""]
RUN dotnet restore "./MyApi.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "MyApi.csproj" -c Release -o /app/build

 

FROM build AS publish
RUN dotnet publish "MyApi.csproj" -c Release -o /app/publish

 

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "MyApi.dll"]
  • Build the Docker image by running the following command in the project folder:
docker run -p 8080:80 myapi

You can access your API by navigating to “http://localhost:8080/api/products”.

In summary, Building APIs .NET CORE is a simple process that allows developers to build powerful and efficient APIs. With platform-independent capabilities and a vast ecosystem of tools and libraries, .NET CORE is a great choice for modern API development. By following the steps in this tutorial, you’ve created the foundation to build more complex APIs to meet the specification application needs.


 


Updated 28-Aug-2023
Economics can be broken down into microeconomics, which looks at individual decisions, and macroeconomics, which is concerned with the economy as a whole. Both types of economics utilize historical trends and current conditions to inform business decision-making and make predictions about how markets might behave in the future. Students who choose to study economics not only gain the skills needed to understand complex markets but come away with strong analytical and problem-solving skills.

Leave Comment

Comments

Liked By